package com.tech.connect.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class IndexImageLayout extends RelativeLayout {

    private ImageViewPager imageViewPager;
    private TextView indexTextView;
    private ArrayList<String> allList = new ArrayList<>();

    public IndexImageLayout(Context context) {
        this(context, null);
    }

    public IndexImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        int dp1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.0f, context.getResources().getDisplayMetrics());
        imageViewPager = new ImageViewPager(context, allList);
        imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indexTextView.setText((position + 1) + "/" + allList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(imageViewPager, params);
        indexTextView = new TextView(context);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);
        layoutParams.addRule(CENTER_HORIZONTAL, TRUE);
        layoutParams.bottomMargin = dp1 * 15;
        indexTextView.setTextColor(Color.WHITE);
        addView(indexTextView, layoutParams);
    }

    public List<String> getAllList() {
        return allList;
    }

    public void updateImages(List<String> allImages, int index) {
        allList.clear();
        if (allImages != null) {
            allList.addAll(allImages);
        }
        imageViewPager.updateImages();
        if (allList.isEmpty()) {
            indexTextView.setText("");
        } else {
            if (index == 0) {
                indexTextView.setText((1) + "/" + allList.size());
            }
        }
        imageViewPager.setCurrentItem(index, true);
    }

    private class ImageViewPager extends ViewPager {

        private ImageAdapter imageAdapter;
        private ArrayList<String> allList;

        public ImageViewPager(Context context, ArrayList<String> allList) {
            super(context, null);
            this.allList = allList;
        }

        public void updateImages() {
            imageAdapter = new ImageAdapter(allList);
            setAdapter(imageAdapter);
        }
    }


    private class ImageAdapter extends PagerAdapter {
        private ArrayList<ImageView> caches = new ArrayList<>();
        private ArrayList<String> allList;

        public ImageAdapter(ArrayList<String> allList) {
            this.allList = allList;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView imageView;
            final String url = allList.get(position);
            if (!caches.isEmpty()) {
                imageView = caches.remove(0);
            } else {
                imageView = new ImageView(container.getContext());
                ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
                imageView.setLayoutParams(layoutParams);
            }
            ImageLoader.getInstance().load(container.getContext(), url, imageView, new RequestOptions().centerCrop());
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
            caches.add((ImageView) object);
        }

        @Override
        public int getCount() {
            return allList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
